﻿@using System.Diagnostics
@using System.Web
@using Masuit.MyBlogs.Core.Common
@using Masuit.MyBlogs.Core.Models.DTO
@using Masuit.MyBlogs.Core.Models.Entity
@using Masuit.MyBlogs.Core.Models.Enum
@using Masuit.MyBlogs.Core.Models.ViewModel
@using Masuit.MyBlogs.Core.Views.Post
@using Masuit.Tools.Core.Net
@using Masuit.Tools.Systems
@model Masuit.MyBlogs.Core.Models.Entity.Post
@{
	ViewBag.Title = Model.Title;
	Layout = "~/Views/Shared/_Layout.cshtml";
	string[] colors = { "success", "info", "primary", "warning", "danger", "default", "primary" };
	UserInfoDto user = Context.Session.Get<UserInfoDto>(SessionKey.UserInfo);
	var cid = Context.Request.RouteValues["cid"] ?? (string.IsNullOrEmpty(Context.Request.Query["cid"]) ? "0" : Context.Request.Query["cid"]);
	Advertisement ad = ViewBag.Ads;
}
<script src="https://cdn.staticfile.org/jqueryui/1.12.1/jquery-ui.min.js" async defer></script>
<environment names="Development">
	<link href="~/Assets/jquery.tocify/jquery.tocify.css" rel="stylesheet" />
	<link href="~/Assets/UEditor/third-party/SyntaxHighlighter/styles/shCore.css" rel="stylesheet" />
	<link href="~/Assets/highlight/css/highlight.css" rel="stylesheet" />
	<script src="/Assets/highlight/js/highlight.js"></script>
	<script src="/Assets/UEditor/third-party/SyntaxHighlighter/scripts/shCore.js"></script>
	<script src="/Assets/UEditor/third-party/SyntaxHighlighter/scripts/bundle.min.js"></script>
	<script src="/Assets/jquery.tocify/jquery.tocify.js"></script>
	<script src="/Scripts/global/article.js"></script>
	<script src="/Assets/highlight/js/highlight.js"></script>
</environment>
<environment names="Stage,Production">
	@await Styles.RenderAsync("/bundles/article.css")
	@await Scripts.RenderAsync("/bundles/article.js")
</environment>
<div class="container">
	<ol class="cd-breadcrumb triangle">
		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
		<li class="current"><em>@ViewBag.Title</em></li>
	</ol>
	<div class="wrapper-content article">
		<div class="ibox">
			<div class="ibox-content animated fadeIn">
				<main>
					<section>
						<header class="page-header">
							<div class="text-center">
								<a>
									<h2 class="padding-bot10">
										@Model.Title
									</h2>
								</a>
							</div>
							<div class="row">
								<div class="col-sm-8">
									<div class="padding-bot10">
										<span class="label label-info">
											<a asp-controller="Home" asp-action="Author" asp-route-author="@Model.Author">@Model.Author</a>
										</span>发表于<span class="text-info">@Model.PostDate.ToString("yyyy-MM-dd HH:mm:ss")</span> |
										<span class="label label-info">
											<a asp-controller="Home" asp-action="Author" asp-route-author="@Model.Modifier">@Model.Modifier</a>
										</span>最后修改于<span class="text-success">@Model.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss")</span>
									</div>
								</div>
								@{
									if (!string.IsNullOrEmpty(Model.Label))
									{
										<div class="pull-right margin-right20">
											<div>
												@foreach (string s in Model.Label.Split(new[] { ',', '，' }, StringSplitOptions.RemoveEmptyEntries))
												{
													<a asp-controller="Home" asp-action="Tag" asp-route-tag="@s">
														<span class="label label-@colors[new Random().Next() % colors.Length]">@s</span>
													</a>
												}
											</div>
										</div>
									}
								}
							</div>
							<div class="row">
								<div class="col-md-12 line-height24">
									分类：<i class="icon-map-pin"></i>
									<a asp-controller="Home" asp-action="Category" asp-route-id="@Model.CategoryId" class="label label-info">@Model.Category.Name</a>
									| 评论总数：<span class="text-danger">@Model.Comment.Count</span>条 | 热度：<span class="text-danger">@Model.TotalViewCount</span>℃ | @(await Html.RenderComponentAsync<PostOnline>(RenderMode.ServerPrerendered, new { Model.Id,IP=Context.Connection.RemoteIpAddress.ToString(),IsAdmin=true })) | 状态：@Model.Status.GetDisplay()
									@if (Model.Seminar.Any())
									{
										<span> | 所属专题：</span>
										var seminars = Model.Seminar;
										foreach (var s in seminars)
										{
											<a asp-controller="Seminar" asp-action="Index" asp-route-id="@s.Id" class="label label-info">@s.Title</a><text> </text>
										}
									}
								</div>
								@if (Model.LimitMode > 0)
								{
									<div class="col-md-6 col-sm-12 text-red margin-top10">
										本文 @string.Format(Model.LimitMode.GetDescription(), Model.Regions, Model.ExceptRegions)
									</div>
								}
								<div class="col-md-6 col-sm-12 pull-right">
									<div class="btn-group pull-right">
										<a asp-controller="Post" asp-action="Refresh" asp-route-id="@Model.Id" class="btn btn-info">刷新</a>
										@if (Model.IsFixedTop)
										{
											<button class="btn btn-success" id="pin">取消置顶</button>
										}
										else
										{
											<button class="btn btn-success" id="pin">置顶</button>
										}
										@if (Model.Status == Status.Pending)
										{
											<button class="btn btn-primary" id="pass">通过</button>
										}
										<a asp-controller="Dashboard" asp-action="Index" asp-fragment="/post/edit?id=@Model.Id" class="btn btn-primary" target="_blank">修改</a>
										<a asp-controller="Dashboard" asp-action="Index" asp-fragment="/writeblog?refer=@Model.Id" class="btn btn-info" target="_blank">复制</a>
										<button class="btn btn-danger" id="del">删除</button>
										<a asp-controller="Post" asp-action="PostVisitRecordInsight" asp-route-id="@Model.Id" class="btn btn-primary" target="_blank">洞察</a>
									</div>
								</div>
							</div>
						</header>
						<article class="article" id="article">
							@if (DateTime.Now - Model.ModifyDate > TimeSpan.FromDays(365))
							{
								<p class="text-focus text-center">该文章已经超过1年未更新，可能无法为您提供及时准确的资讯，请根据当下实际情况，酌情参考本文内容。</p>
							}
							@Html.Raw(Model.Content)
							@Html.Raw(Model.ProtectContent)
						</article>
					</section>
				</main>
				<hr class="margin-top10 marginbot10" />
				@if (ad != null)
				{
					await Html.RenderPartialAsync("_ArticleListAdvertisement", ad);
				}
				<section class="wow fadeIn row padding-top40 padding-bot20">
					<div class="col-xs-6">
						<div class="btn-group">
							<button type="button" id="voteup" class="btn btn-success btn-lg">
								<i class="icon-thumbsup"></i><span>@Model.VoteUpCount</span>
							</button>
							<button type="button" id="votedown" class="btn btn-danger btn-lg">
								<i class="icon-thumbsdown"></i><span>@Model.VoteDownCount</span>
							</button>
						</div>
					</div>
				</section>
				<section class="wow row size16 animated fadeIn">
					<div class="col-xs-6">
						上一篇：
						@{
							PostModelBase prev = ViewBag.Prev;
							if (prev != null)
							{
								<a asp-controller="Post" asp-action="Details" asp-route-id="@prev.Id">@prev.Title</a>
							}
							else
							{
								<a>没有了</a>
							}
						}
					</div>
					<div class="col-xs-6 text-right">
						下一篇：
						@{
							PostModelBase next = ViewBag.Next;
							if (next != null)
							{
								<a asp-controller="Post" asp-action="Details" asp-route-id="@next.Id">@next.Title</a>
							}
							else
							{
								<a>没有了</a>
							}
						}
					</div>
				</section>
				@if (Model.PostHistoryVersion.Any())
				{
					<section class="wow margintop20 animated fadeIn">
						<h3>文章历史版本：</h3>
						<p>
							修改次数：@Model.PostHistoryVersion.Count 次
							<a asp-controller="Post" asp-action="History" asp-route-id="@Model.Id">查看历史版本</a>
						</p>
					</section>
				}
				@{
					var posts = (Dictionary<int, string>)ViewBag.Related;
					if (posts.Count > 1)
					{
						<section class="wow margintop20 animated fadeIn">
							<h3>相关推荐：</h3>
							<table class="table table-condensed size16">
								@for (int i = 1; i < posts.Count; i += 2)
								{
									<tr>
										<td style="width: 50%"><a asp-controller="Post" asp-action="Details" asp-route-id="@posts.ElementAt(i - 1).Key">@posts.ElementAt(i - 1).Value</a></td>
										<td><a asp-controller="Post" asp-action="Details" asp-route-id="@posts.ElementAt(i).Key">@posts.ElementAt(i).Value</a></td>
									</tr>
								}
							</table>
						</section>
					}
				}
				<section class="wow padding-bot20 animated fadeIn">
					<h3>版权声明：</h3>
					<p class="text-red size16">
						@if (Model.DisableCopy)
						{
							<text>🈲⚠本文为作者原创，仅用于本站访客学习、研究和交流目的，未经授权禁止转载。️⚠🈲</text>
						}
						else
						{
							<text>本文仅用于学习、研究和交流目的，欢迎非商业性质转载。本文链接：<a asp-controller="Post" asp-action="Details" asp-route-id="@Model.Id">@HttpUtility.UrlDecode(Context.Request.Scheme + "://" + Context.Request.Host + Context.Request.Path)</a>。</text>
							@Html.Raw(CommonHelper.SystemSettings["Disclaimer"])
						}
					</p>
				</section>
				<section class="wow row animated fadeIn">
					<div class="col-lg-12">
						<h3>评论区：</h3>
						<form class="form-horizontal animated pulse" id="comment" method="post">
							@Html.AntiForgeryToken()
							<input type="hidden" name="PostId" value="@Model.Id" />
							<input type="hidden" id="OperatingSystem" name="OperatingSystem" />
							<input type="hidden" name="Browser" id="Browser" />
							<input type="hidden" value="@user.NickName" class="form-control" name="NickName" id="name">
							<input type="hidden" value="@user.Email" class="form-control" name="Email" id="email">
							<div class="form-group overlay animated fadeInDown" style="margin-bottom: 0px !important;">
								<textarea class="layui-textarea" id="layedit" name="Content" style="height: 100px"></textarea>
							</div>
							<input type="hidden" name="Agree" value="true" />
							<div class="form-group">
								<div class="col-xs-12">
									<button type="submit" class="btn btn-info btn-lg">提交</button>
									<a class="text-red">评论框可粘贴网络图片</a>
								</div>
							</div>
						</form>
						<ul class="wow media-list"></ul>
						@if (Model.Comment.Any(c => c.Status == Status.Published))
						{
							<div class="row">
								<div class="col-md-12 text-center">
									<div id="pageToolbar"></div>
								</div>
							</div>
						}
						else
						{
							<h4>还没有评论哦，赶紧来写评论吧</h4>
						}
					</div>
				</section>
			</div>
		</div>
	</div>
</div>
<div style="position: absolute; left: -20000px; bottom: 0;">
	<div id="reply" class="container-fluid">
		<form class="form-horizontal" id="reply-form" method="post">
			@Html.AntiForgeryToken()
			<input type="hidden" name="PostId" id="postId" value="@Model.Id" />
			<input type="hidden" name="OperatingSystem" id="OperatingSystem2" />
			<input type="hidden" name="Browser" id="Browser2" />
			<input type="hidden" value="@user.NickName" class="form-control" name="NickName" id="name2">
			<input type="hidden" value="@user.Email" class="form-control" name="Email" id="email2">
			<input type="hidden" value="" id="uid" name="ParentId" />
			<div class="form-group overlay animated fadeInDown">
				<textarea class="layui-textarea" id="layedit2" name="Content" style="height: 80px"></textarea>
			</div>
			<input type="hidden" name="Agree" value="true" />
			<div class="form-group">
				<div class="col-xs-12">
					<div class="btn-group">
						<button type="submit" class="btn btn-info btn-lg">
							提交回复
						</button>
						<button type="button" class="btn-cancel btn btn-danger btn-lg">
							取消回复
						</button>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>
<script>
	function getcomments() {
		window.get("/comment/getcomments?id=" + $("#postId").val()+"&page=1&size=10&cid="+@cid,function(data) {
			data = data.Data;
			document.querySelector(".media-list").innerHTML = loadParentComments(data);
			bindReplyBtn();
			commentVoteBind(); //评论投票
		});
	}
	window.onload = function() {
		const keywords = @Html.Raw(ViewData["keywords"] ?? "[]");
		for (let i = 0; i < keywords.length; i++) {
			$("#article p").highlight(keywords[i]);
			$("#article div").highlight(keywords[i]);
			$("#article h3").highlight(keywords[i]);
			$("#article h4").highlight(keywords[i]);
		}
		loading();
		$('#pageToolbar').Paging({ //异步加载评论
			pagesize: 10,
			count: @Model.Comment.Count(c =>c.ParentId==0&& c.Status==Status.Published),
			toolbar: true,
			callback: function(page, size, count) {
				window.get("/comment/getcomments?id=" +@Model.Id+"&page=" + page + "&size=" + size, function (data) {
					data = data.Data;
					if (data) {
						document.querySelector(".media-list").innerHTML = loadParentComments(data);
						bindReplyBtn();
						commentVoteBind(); //评论投票
					}
				});
			}
		});
		$("#del").on("click", function (e) {
			swal({
				title: "确认删除这篇文章吗？",
				text: '@Model.Title',
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				showLoaderOnConfirm: true,
				animation: true,
				allowOutsideClick: false
			}).then(function () {
				window.post("/post/delete", { id: @Model.Id}, function (data) {
					window.notie.alert({
						type: 1,
						text: data.Message,
						time: 4
					});
				}, () => {
					window.notie.alert({
						type: 3,
						text: "请求失败，请稍候再试！",
						time: 4
					});
				});
			}, function () {
			});
		});
		$("#pass").on("click", function (e) {
			window.post("/post/pass", { id:@Model.Id}, function (data) {
				window.notie.alert({
					type: 1,
					text: data.Message,
					time: 4
				}, () => {
					window.notie.alert({
						type: 3,
						text: "请求失败，请稍候再试！",
						time: 4
					});
				});
				location.reload();
			});
		});
		$("#pin").on("click", function (e) {
			window.post("/post/Fixtop", { id: @Model.Id }, function (data) {
				window.notie.alert({
					type: 1,
					text: data.Message,
					time: 4
				}, () => {
					window.notie.alert({
						type: 3,
						text: "请求失败，请稍候再试！",
						time: 4
					});
				});
				location.reload();
			});
		});
		loadingDone();
	};
	//递归加载评论
	//加载父楼层
	function loadParentComments(data) {
		loading();
		var html = '';
		if (data) {
			var rows = data.rows;
			var page = data.page;
			var size = data.size;
			var maxPage = Math.ceil(data.total / size);
			page = page > maxPage ? maxPage : page;
			page = page < 1 ? 1 : page;
			var startfloor = data.parentTotal - (page - 1) * size;
			for (let i = 0; i < rows.length; i++) {
				html += `<li class="msg-list media animated fadeInRight" id='${rows[i].Id}'>
							<div class="media-body">
								<article class="panel panel-info">
									<header class="panel-heading ${rows[i].IsMaster ? "text-red" : ""} ${rows[i].IsAuthor ? "text-bold" : ""}">${startfloor--}# ${rows[i].IsMaster ? `<i class="icon icon-user"></i>` : ""}${rows[i].NickName}${rows[i].IsMaster ? `(管理员)` : ""} | ${rows[i].CommentDate}
										<span class="pull-right" style="font-size: 10px;">${rows[i].Status == 4 ? `<a class="label label-success" onclick="pass(${rows[i].Id})">通过</a> |` : ""} <a class="label label-danger" onclick="del(${rows[i].Id})">删除</a> | ${GetOperatingSystem(rows[i].OperatingSystem) + " | " + GetBrowser(rows[i].Browser)}</span>
									</header>
									<div class="panel-body">
										${rows[i].Content}
										<span class="cmvote label label-info" data-id="${rows[i].Id}"><i class="icon-thumbsup"></i>(<span class="count">${rows[i].VoteCount}</span>)</span>
										<a class="label label-info" href="?uid=${rows[i].Id}"><i class="icon-comment"></i></a><div class="margin-top10"></div>
										<div class="pull-left">
											<span class="label label-success">${rows[i].IP}</span>
											<span class="label label-primary">${rows[i].Location}</span>
										</div>
										<div class="pull-right">
											<span class="label label-success" onclick="bounceEmail('${rows[i].Email}')">${rows[i].Email}</span>
										</div><br/>
										${loadComments(rows[i].Children)}
									</div>
								</article>
							</div>
						</li>`;
			}
		}
		loadingDone();
		return html;
	}

	//加载子楼层
	function loadComments(comments, depth = 0) {
		comments.sort(function(x, y) {
			return x.Id - y.Id
		});

		var colors = ["info", "success", "primary", "warning", "danger"];
		var floor = 1;
		depth++;
		var html = '';
		for (let item of comments) {
			var color = colors[depth % 5];
			html += `<article id="${item.Id}" class="panel panel-${color}">
							<div class="panel-heading ${item.IsMaster ? "text-red" : ""} ${item.IsAuthor ? "text-bold" : ""}">
								${depth}-${floor++}# ${item.IsMaster ? `<i class="icon icon-user"></i>` : ""}${item.NickName}${item.IsMaster ? `(管理员)` : ""} | ${item.CommentDate}
								<span class="pull-right" style="font-size: 10px;">${item.Status == 4 ? `<a class="label label-success" onclick="pass(${item.Id})">通过</a> |` : ""} <a class="label label-danger" onclick="del(${item.Id})">删除</a> | ${GetOperatingSystem(item.OperatingSystem) + " | " + GetBrowser(item.Browser)}</span>
							</div>
							<div class="panel-body">
								${item.Content}
								<span class="cmvote label label-${color}" data-id="${item.Id}"><i class="icon-thumbsup"></i>(<span class="count">${item.VoteCount}</span>)</span>
								<a class="label label-${color}" href="?uid=${item.Id}"><i class="icon-comment"></i></a>
								<div class="margin-top10"></div>
								<div class="pull-left">
									<span class="label label-success">${item.IP}</span>
									<span class="label label-primary">${item.Location}</span>
								</div>
								<div class="pull-right">
									<span class="label label-success" onclick="bounceEmail('${item.Email}')">${item.Email}</span>
								</div><br/>
								${loadComments(item.Children, depth)}
							</div>
						</article>`;
		}
		return html;
	}

	function pass(id) {
		window.post("/comment/pass", { id: id }, function (res) {
			swal(res.Message, "", res.Success ? "success" : "error");
			getcomments();
		}, () => {
			swal("操作失败，请稍候再试", "", "error");
		});
	}

	function bounceEmail(email) {
		swal({
			title: "确定将这个邮箱添加到黑名单吗？",
			showCancelButton: true,
			confirmButtonColor: "#DD6B55",
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			showLoaderOnConfirm: true,
			animation: true,
			allowOutsideClick: false
		}).then(function () {
			window.post("/system/BounceEmail", { email: email }, function () {
				swal("邮箱添加到黑名单成功", "", "success");
			}, (e) => {
				swal("操作失败，请稍候再试", "", "error");
			});
		}, function () { });
	}

	function del(id) {
		swal({
			title: "确定删除这条评论吗？",
			showCancelButton: true,
			confirmButtonColor: "#DD6B55",
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			showLoaderOnConfirm: true,
			animation: true,
			allowOutsideClick: false
		}).then(function () {
			$.post("/comment/delete", {
				id: id
			}, function (res) {
				swal(res.Message, "", res.Success ? "success" : "error");
				getcomments();
			});
		}, function () { });
	}

function showViewer(data) {
	var html="";
	for (let item of data) {
		html+=`<p><a href="/tools/ip/${item.key}" target="_blank">${item.key}</a>：${item.value}</p>`;
	}
	layer.open({
	  type: 1,
	  area: ['800px',"60%"], //宽高
	  content: html
	});
}
</script>